﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Interface;
using App.Model;
using App.Model.MoBanModel;
using Newtonsoft.Json;

namespace App.MVC.Controllers
{
    public class KeShiConfigController : BaseController
    {
        // GET: KeShiConfig
        public IKeShi _IKeShi { set; get; }
        public IMoBan _IMoBan { set; get; }
        public IZhiWu _IZhiWu { set; get; }
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAllKeShi()
        {
            //return Json(dynamic, JsonRequestBehavior.AllowGet);
            var str = _IKeShi.GetAllKeShiList();
            List<KeShi> tt = JsonConvert.DeserializeObject<List<KeShi>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetKeShiZhiWu(int keshiid)
        {
            var lst = _IKeShi.GetKeShiZhiWu(keshiid);
            Dictionary<string, TreeZhiWu> dic = new Dictionary<string, TreeZhiWu>();
            var datalist = JsonConvert.DeserializeObject<List<ZhiWu>>(lst);
            var zwids = datalist.Select(s => s.zwid).ToList();
            var topzhiwu = new List<ZhiWu>();
            if (datalist.Any(s => s.PZhiWei == null))
            {
                topzhiwu = datalist.Where(s => s.PZhiWei == null).ToList();
            }
            else
            {
                topzhiwu = datalist.Where(s => !zwids.Exists(p => p == s.PZhiWei.zwid)).ToList();
            }

            foreach (var item in topzhiwu)
            {
                TreeZhiWu model = new TreeZhiWu();
                model.text = item.zwmc;
                model.id = item.zwid;
                model.type = "folder";
                //model.gzjhid = item.gzjhid;
                //model.jxkhid = item.jxkhid;
                model.pid = 0;
                model.additionalParameters = new Dictionary<string, Dictionary<string, TreeZhiWuSon>>();
                Dictionary<string, TreeZhiWuSon> tmp1 = new Dictionary<string, TreeZhiWuSon>();
                foreach (var item2 in datalist.Where(s => s.PZhiWei != null && s.PZhiWei.zwid == item.zwid))
                {
                    TreeZhiWuSon sonmodel = new TreeZhiWuSon
                    {
                        id = item2.zwid,
                        text = item2.zwmc,
                        type = "item",
                        //gzjhid = item2.gzjhid,
                        //jxkhid = item2.jxkhid,
                        pid = item.zwid,
                        PowerTrees = item2.PowerTrees
                        //PowerTrees=new LinkedList<)>()
                    };
                    tmp1.Add(Guid.NewGuid().ToString(), sonmodel);
                }
                model.additionalParameters.Add("children", tmp1);
                model.PowerTrees = item.PowerTrees;
                dic.Add(Guid.NewGuid().ToString(), model);
            }

            return Json(dic, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public void UpdateZhiWuPowerTrees(int ksid, int zwid, string ptmc)
        {
            _IKeShi.UpdateZhiWuPowertrees(ksid, zwid, ptmc);
        }


        [HttpPost]
        public void SaveKeShi(int ksid, string ksmc)
        {
            if (ksid == 0)
            {
                _IKeShi.AddKeShi(ksmc);
            }
            else
            {
                _IKeShi.UpdateKeShi(ksid, ksmc);
            }
        }
        [HttpGet]
        public void DelKeShi(int ksid)
        {
            _IKeShi.DelKeShi(ksid);
        }
        [HttpGet]
        public JsonResult GetKeShiZhiWuOne(int ksid)
        {
            //var str = _IKeShi.GetKeShiZhiWu(ksid);
            var str = _IZhiWu.GetZhiWuList();
            List<ZhiWu> tt = JsonConvert.DeserializeObject<List<ZhiWu>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetGongZuoJiHuo()
        {
            var str = _IMoBan.GetMoBanList();
            List<MoBan_Main> tt = JsonConvert.DeserializeObject<List<MoBan_Main>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetJiXiaoList()
        {
            var str = _IKeShi.GetJiXiaoList();
            List<MoBan> tt = JsonConvert.DeserializeObject<List<MoBan>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public void SaveZhiWu(int ksid, int zwid)
        {
            _IKeShi.AddZhiWu(ksid, zwid);

        }
        [HttpGet]
        public void DelZhiWu(int ksid, int zwid)
        {
            _IKeShi.DelZhiWu(ksid, zwid);
        }

        [HttpGet]
        public JsonResult GetQuanXian()
        {
            var str = _IKeShi.GetQuanXian();
            List<App_PowerTree> lst = JsonConvert.DeserializeObject<List<App_PowerTree>>(str);
            return Json(lst, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public void SaveZhiWuKaoHeBiao(int ksid, int zwid, int khbid)
        {
            _IKeShi.KaoHeBiaoRelationZhiWu(ksid, zwid, khbid);
        }

        [HttpPost]
        public void DelZhiWuKaoHeBiao(int ksid, int zwid, int khbid)
        {
            _IKeShi.DelKaoHeBiaoRelationZhiWu(ksid, zwid, khbid);
        }

        [HttpPost]
        public JsonResult GetZhiWuKaoHeBiaos(int ksid, int zwid)
        {
            var str = _IKeShi.GetZhiWuKaoHeBiao(ksid, zwid);
            List<MoBan_Main> lst = JsonConvert.DeserializeObject<List<MoBan_Main>>(str);
            return Json(lst, JsonRequestBehavior.AllowGet);
        }
        #region 职务管理

        public ActionResult ZhiWuGuanLi()
        {
            return View();
        }
        #endregion
    }
}